﻿package dhcpha.clinical.dal;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

/**
 * 
 * @ClassName:	WsApiUtil 
 * @Description: webservice接口类
 * @author:	bianshuai
 * @date:	2017-4-18 下午6:18:40 
 *
 */
public class WsApiUtil {
	
	// 定义Web Service的命名空间
	static final String SERVICE_NS = "http://www.dhccmedical.com.cn";
	static final String METHOD_NAME = "InvCmPubService";

	/**
	 * 
	 * 接收服务器返回的SoapObject数据
	 * 
	 * @param serviceUrl
	 * @param Parrm
	 * @return
	 * @throws Exception
	 */
	public static String loadSoapObject(String serviceUrl, Map Parrm) throws Exception {
		
		String retData = null;

		// 创建soapObject对象
		SoapObject soapObject = new SoapObject(SERVICE_NS, METHOD_NAME);
		// 设置参数
		Iterator iter = Parrm.entrySet().iterator();// 先获取这个map的set序列，再或者这个序列的迭代器
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next(); // 得到这个序列的映射项，就是set中的类型，HashMap都是Map.Entry类型（详情见map接口声明）
			soapObject.addProperty(entry.getKey().toString(), entry.getValue());
		}
		
		// 创建SoapSerializationEnvelope对象，并设置WebService版本号
		SoapSerializationEnvelope serializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		// 设置serializationEnvelope对象的badyOut属性
		serializationEnvelope.bodyOut = soapObject;
		// 创建HttpTransportSE对象,并且确定wsdl网络地址
		HttpTransportSE httpTransportSE = new HttpTransportSE(serviceUrl);

		try {
			// httpTransportSE调用Call方法
			httpTransportSE.call(SERVICE_NS + "/" + METHOD_NAME, serializationEnvelope);
			// 获取返回的结果对象
			if (serializationEnvelope.getResponse() != null) {
				SoapObject result = (SoapObject) serializationEnvelope.bodyIn;
				Object obj = result.getProperty(METHOD_NAME + "Result");
				retData =obj.toString();
			}

		}catch (IOException e) {
			throw e;
		}catch (XmlPullParserException e2) {
			throw e2;
		}catch (Exception e3) {
			throw e3;
		}finally{
			httpTransportSE.getServiceConnection().disconnect();
			httpTransportSE = null;
		}

		return retData;
	}

}
